﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PrjAssess.Models;
using PrjAssess.Models.Service.AssessProject;
using PrjAssess.Models.Service.DoAssessProject;
using PrjAssessModels.BaseModels;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PrjAssess.Controllers
{
    //对当前工程进行测算
    public class DoAssessController : Controller
    {
        //
        // GET: /DoAssess/

        public ActionResult Index()
        {
            if (GlobalData.PrjId == null || GlobalData.PrjId == string.Empty)
            {
                return RedirectToAction("Index", "ProjectManage");
            }
            Project prj = ProjectDataHelper.GetByPrjId(GlobalData.PrjId);
            return View(prj);
        }


        public ActionResult EditBasicInfo()
        {
            Project prj = ProjectDataHelper.GetPrjBasicInfoOnly(GlobalData.PrjId);
            prj.FromExcel = false;
            return View(prj);
        }

        [HttpPost]
        public JsonResult SaveBasicInfo(string prjJson)
        {
            Project prj = Newtonsoft.Json.JsonConvert.DeserializeObject<Project>(prjJson);
            ProjectDataHelper.SavePrjBasicInfoOnly(prj);
            return Json("1");
        }
        /// <summary>
        /// 获取工程的评测信息
        /// </summary>
        /// <param name="prjId">工程Id</param>
        /// <param name="title">评测项的名称</param>
        /// <returns>List of AssessItem</returns>
        [HttpGet]
        public string GetAssessItems(string prjId, string title)
        {
            var list = AssessItemsHelper.GetAssessItemsByTitle(prjId, title);
            return JsonConvert.SerializeObject(list);
        }

        //材料埋设考核
        public ActionResult Mertial()
        {
            if (GlobalData.PrjId == null || GlobalData.PrjId == string.Empty)
            {
                return RedirectToAction("Index", "ProjectManage");
            }
            Project prj = ProjectDataHelper.GetByPrjId(GlobalData.PrjId);
            ViewBag.PrjId = GlobalData.PrjId;

            return View(prj);
        }

        //控制成本预算考核
        public ActionResult Cost()
        {
            if (GlobalData.PrjId == null || GlobalData.PrjId == string.Empty)
            {
                return RedirectToAction("Index", "ProjectManage");
            }
            Project prj = ProjectDataHelper.GetByPrjId(GlobalData.PrjId);
            ViewBag.PrjId = GlobalData.PrjId;
            var paraHelper = new ParameterHelper(prj.PrjId);
            ViewBag.Cost = Math.Round(prj.WorkSum * paraHelper["预算控制成本", "核定工作量"] / 100.0f, 2);
            return View(prj);
        }

        //管理考核
        public ActionResult Administration()
        {
            if (GlobalData.PrjId == null || GlobalData.PrjId == string.Empty)
            {
                return RedirectToAction("Index", "ProjectManage");
            }
            Project prj = ProjectDataHelper.GetByPrjId(GlobalData.PrjId);
            ViewBag.PrjId = GlobalData.PrjId;
            ViewBag.Cost = prj.WorkSum;
            return View(prj);
        }

        //工作量考核
        public ActionResult Work()
        {
            if (GlobalData.PrjId == null || GlobalData.PrjId == string.Empty)
            {
                return RedirectToAction("Index", "ProjectManage");
            }
            Project prj = ProjectDataHelper.GetByPrjId(GlobalData.PrjId);
            ViewBag.PrjId = GlobalData.PrjId;
            //参数获取
            ParameterHelper pl = new ParameterHelper(prj.PrjId);
            var msg = new
            {
                paraCompany = pl["项目部额度","核定工作量"] / 100,
                paraTeam = pl["班组额度", "核定工作量"] / 100,
                workSum = prj.WorkSum
            };
            ViewBag.msg = Newtonsoft.Json.JsonConvert.SerializeObject(msg);
            return View(prj);
        }
        //参数
        public ActionResult Para()
        {
            if (GlobalData.PrjId == null || GlobalData.PrjId == string.Empty)
            {
                return RedirectToAction("Index", "ProjectManage");
            }

            Project prj = ProjectDataHelper.GetByPrjId(GlobalData.PrjId);
            ViewBag.PrjId = GlobalData.PrjId;
            ParameterHelper helper = new ParameterHelper(prj.PrjId);
            //分组
            var groups = from p in helper.ParaItems
                         group p by p.GroupName into g
                         select new
                         {
                             key = g.Key,
                             list = g.ToArray()
                         };
            ViewBag.groups = Newtonsoft.Json.JsonConvert.SerializeObject(groups);
            return View();
        }
    
        /// <summary>
        /// 添加考核项
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        [HttpPost]
        public string insertAssessItem(string jsonStr)
        {
            var insertObject = Newtonsoft.Json.JsonConvert.DeserializeObject<AssessItem>(jsonStr);
            using (PrjAssessDb db = new PrjAssessDb())
            {
                try
                {
                    // 写数据库
                    db.AssessItems.Add(insertObject);
                    db.SaveChanges();
                }
                catch (DbEntityValidationException dbEx)
                {
                    throw dbEx;
                }
               
                return "1";
            }
        }
        [HttpPost]
        public string updatePara(string jsonArray,string prjId)
        {
            var array = JsonConvert.DeserializeObject<ParaItem[]>(jsonArray);
            var helper = new ParameterHelper(prjId);
            helper.Update(array);
            return "1";
        }

        [HttpPost]
        public string insertAssessItemArray(string jsonArray)
        {
            var a = JArray.Parse(jsonArray);
        
            using (PrjAssessDb db = new PrjAssessDb())
            {
                try
                {
                    // 写数据库
                    foreach (var i in a)
                    {
                        var ob = i.ToObject<AssessItem>();
                        var item = db.AssessItems.FirstOrDefault(p => p.PrjId == ob.PrjId && p.Time.Equals(ob.Time)
                        && p.Title == ob.Title && p.SubTitle == ob.SubTitle);
                        if (item != null) db.AssessItems.Remove(item);
                        db.AssessItems.Add(ob);
                    }
                    db.SaveChanges();
                }
                catch (DbEntityValidationException dbEx)
                {
                    throw dbEx;
                }

                return "1";
            }
        }

        /// <summary>
        /// 删除考核项,同日期的话会被全部删除
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        [HttpPost]
        public string deleteAssessItem(string jsonStr)
        {
            var ob = Newtonsoft.Json.JsonConvert.DeserializeObject<AssessItem>(jsonStr);
            using (PrjAssessDb db = new PrjAssessDb())
            {
                try
                {
                    var item = db.AssessItems.FirstOrDefault(p => p.PrjId == ob.PrjId && p.Time.Equals(ob.Time)
                        && p.Title == ob.Title && p.SubTitle == ob.SubTitle);
                    db.AssessItems.Remove(item);
                    db.SaveChanges();
                }
                catch (DbEntityValidationException dbEx)
                {
                    throw dbEx;
                }

                return "1";
            }
        }

    }
}
